/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cassandrawindow;

import java.io.IOException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.crypto.Mac;

public class HandleDataTableBannerDateV3 {
    
    public static final String KEYSPACE                             =   "AdsData";
    public static final String ColumFamly                           =   "BannerViewCountCf";
    public static final String ColumFamlyBannerClickCountCf         =   "BannerClickCountCf";
    public static final String ColumFamlyBannerViewCountCf          =   "BannerViewCountCf";

    public static final String ColumFamlyCampaignClickCountCf       =   "CampaignCountCf";
    public static final String ColumFamlyCampaignViewCountCf        =   "CampaignViewCountCf";
    
     public static final String ColumFamlyDateBanner_Geo_Counter    =   "DateBanner_Geo_Counter";
    
    public void handleData() throws ParseException, InterruptedException
    {
        MysqlDataManager67 mysql67 = new MysqlDataManager67();
        CassandraAction cassAct = new CassandraAction(ACCOUNT.CLUSTERNAME ,ACCOUNT.URL, ACCOUNT.PORT);
        cassAct.initReadWirte(KEYSPACE);
        
        String currentDate = HandleDataTableDomainDate.handleDate();//2012-07-24
        //Debug.println("start time :" +TimeUtil.getCurrentTimeInStringForLog());                
        //String currentDate = "2013-08-05";        
        long first_time = System.currentTimeMillis();               
        MysqlDataManager mysqldatamanager = new MysqlDataManager();
        //listCampaigns = mysqldatamanager.getCampaignIdBySliceCPM(currentDate);                
        //get all fraud click by date
        Map<String, Integer> fraudclicks = new HashMap<>();       
        fraudclicks = mysqldatamanager.getAllFraudClickByBannerid(currentDate);
        
        Map<String, Long> MoneyByBanner = new HashMap<>();
        MoneyByBanner = mysql67.getMoneyByBannerId(currentDate);
        //max value
        Map<Integer, Long> MaxValue = new HashMap<>();
        MaxValue = mysql67.getMaxValueDaybyBannerIdCPC(currentDate);
        //get all bannerid stop
        List allBannerStopAndPause = new ArrayList();        
        //allBannerStopAndPause = mysqldatamanager.getAllBanneridStoporPause(currentDate);        
        //geo valid
        /*Map<String,String> geoValid = new HashMap<String,String>();
        geoValid = CheckGeoByBannerid.handleData();*/
        
        //tinh Geo     
        Map<String, String> geoMap = new HashMap<>();
        geoMap = HandleDataGeo.handleData();
                
        //lay du lieu tu bang domain
        Map<String, String> dataFromTableDomain = new HashMap<>();        
        
        dataFromTableDomain = mysql67.getClickByDomainDateCPCandCPCplus(currentDate);
        StringBuilder sbff = new StringBuilder();
        Iterator iterator = dataFromTableDomain.entrySet().iterator();
         //get number click by domain date
        Map<String, Long> ClickByDomainDate = new HashMap<>();
        ClickByDomainDate = mysql67.getClickByDomainDate(currentDate);
        //chua tinh voi khi compare
        
        Map<Integer, String> uv_uc = new HashMap<>();              
        uv_uc = mysql67.getUvUcByBannerid(currentDate);
        //get price by domain
        Map<String, Integer> PriceByDomain = new HashMap<>();
        PriceByDomain = mysql67.getPriceByDomainCPC();
        
        //get all banid : campid
        Map<String, String> banAndCamp = new HashMap<>();
        banAndCamp = mysql67.getBannerIDandCapmIDByCPCandCPCPlus();
                        
        //get bannerid completed
        Map<String,String> BannerComplete= new HashMap<String,String>();
        BannerComplete = mysql67.getBannerComplete(currentDate);
        
        Map<String, String> banAndCampEnbacSolo = new HashMap<>();
        banAndCampEnbacSolo = mysql67.getBannerIDandCapmIDByCPCandCPCPlusSoloEnbac();
        
        //get banner cua Boxvideo AF
        Map<String, String> banAndCampBoxvideoAF = new HashMap<>();
        banAndCampBoxvideoAF = mysql67.getBannerIDandCapmIDByBoxvideoAF();
                
        Map<String, String> ClickAndMoneyByCamp = new HashMap<>();
        ClickAndMoneyByCamp = mysql67.getClickByCampaignid(currentDate);
                       
	while (iterator.hasNext()) {
                Map.Entry mapEntry = (Map.Entry) iterator.next();
                
                String key = (String) mapEntry.getKey();
                String value = (String) mapEntry.getValue();
                String bannerid     = key.split("_")[0];        
                
                try {
                    if(banAndCamp.containsKey(bannerid))
                    {
                        String camid        = banAndCamp.get(bannerid);

                        //String bannerid     = "187589";
                        String totalclick   = value.split("_")[0];
                        String totalview    = value.split("_")[1];
                        String money        = value.split("_")[2];
                        String geoValue     = geoMap.get(bannerid);
                        //String geoVal       = geoValid.get(bannerid); 

                        String str = getDataCampaignAndBannerID(currentDate, camid, bannerid,Integer.valueOf(totalclick),Integer.valueOf(totalview),Integer.valueOf(money),fraudclicks,MoneyByBanner,MaxValue,allBannerStopAndPause,uv_uc,geoValue,ClickByDomainDate,PriceByDomain,ClickAndMoneyByCamp);
                        sbff.append(str);                        
                    }                
                    /*
                    * lay nhung bannerid hoan thanh trong ngay
                    */
                    else if(BannerComplete.containsKey(bannerid))
                    {
                        String camid        = BannerComplete.get(bannerid);

                        //String bannerid     = "187589";
                        String totalclick   = value.split("_")[0];
                        String totalview    = value.split("_")[1];
                        String money        = value.split("_")[2];
                        String geoValue     = geoMap.get(bannerid);
                        /*String geoValue ="";
                        if(geoMap.containsKey(bannerid)){
                            geoValue     = geoMap.get(bannerid);
                        //String geoVal       = geoValid.get(bannerid); 
                        }*/
                        String str = getDataCampaignAndBannerID(currentDate, camid, bannerid,Integer.valueOf(totalclick),Integer.valueOf(totalview),Integer.valueOf(money),fraudclicks,MoneyByBanner,MaxValue,allBannerStopAndPause,uv_uc,geoValue,ClickByDomainDate,PriceByDomain,ClickAndMoneyByCamp);
                        sbff.append(str);                        
                    }
                } catch(Exception ex){
                    Debug.println("exception:"+ex.getMessage());                     
                }

                /*
                 * lay nhung campaign cua solo va enbac
                 */
                if(banAndCampEnbacSolo.containsKey(bannerid)){
                    String camid        = banAndCampEnbacSolo.get(bannerid);

                    //String bannerid     = "187589";
                    String totalclick   = value.split("_")[0];
                    String totalview    = value.split("_")[1];
                    String money        = value.split("_")[2];
                    String geoValue     = geoMap.get(bannerid);
                    //String geoVal = geoValid.get(bannerid); 
                    
                    String str = getDataCampaignAndBannerID(currentDate, camid, bannerid,Integer.valueOf(totalclick),Integer.valueOf(totalview),Integer.valueOf(money),fraudclicks,MoneyByBanner,MaxValue,allBannerStopAndPause,uv_uc,geoValue,ClickByDomainDate,PriceByDomain,ClickAndMoneyByCamp);
                    sbff.append(str);  
                }                  
                /*
                 * lay nhung campaignid cua videobox AF
                 */
                if(banAndCampBoxvideoAF.containsKey(bannerid)){
                    String camid        = banAndCampBoxvideoAF.get(bannerid);

                    //String bannerid     = "187589";
                    String totalclick   = value.split("_")[0];
                    String totalview    = value.split("_")[1];
                    String money        = value.split("_")[2];
                    String geoValue     = geoMap.get(bannerid);
                    //String geoVal = geoValid.get(bannerid); 
                    
                    String str = getDataCampaignAndBannerID(currentDate, camid, bannerid,Integer.valueOf(totalclick),Integer.valueOf(totalview),Integer.valueOf(money),fraudclicks,MoneyByBanner,MaxValue,allBannerStopAndPause,uv_uc,geoValue,ClickByDomainDate,PriceByDomain,ClickAndMoneyByCamp);
                    sbff.append(str);  
                }  
	}                        
        
        //xu li cho du lieu cuoi
        if(sbff.toString().length() > 1)
        {           
            String relStr = sbff.toString();
            String valInsert = relStr.substring(0, (relStr.length()-1));
            
            HandleImportData hid = new HandleImportData();
            String pathFile="";
            try {
                pathFile = hid.outToCsvBanner(valInsert);
                Debug.println("pathFile:"+pathFile);
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            try {
                //get sys_date_cpc
                String strDate =  mysql67.getSysDateCPC();                              
                MysqlDataManager67 mysql672 = new MysqlDataManager67(false);
                mysql672.beforeLoadData();                               
                mysql672.dropPartition(CONSTANT.V3_BAN_DATE_CS, strDate);                          
                                        
                mysql672.loadToV3BanDateFromFile(pathFile);    
                //unlock
                //mysql672.unlockTable();
                mysql672.shutdownConnection();                
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        //clear map
        
        //close connction to db
        mysqldatamanager.shutdownConnection();                
        mysql67.shutdownConnection();
        
        long last_time = System.currentTimeMillis();
        Debug.println("Thoi gian chay  :" +(last_time-first_time));
        Debug.println("success HandleDataTableBannerDate");

    }
    public static void removeDuplicateWithOrder(ArrayList arlList)
    {
        Set set = new HashSet();
        List newList = new ArrayList();
        for (Iterator iter = arlList.iterator(); iter.hasNext(); ) {
            Object element = iter.next();
            if (set.add(element))
            newList.add(element);
        }
        arlList.clear();
        arlList.addAll(newList);
    }
    public static String getDataCampaignAndBannerID(String currentDay,String camId,String banId,int totalclick,int totalview,int money,Map<String, Integer> fraudclicks,Map<String, Long> MoneyByBanner,Map<Integer, Long> MaxValue,List allBannerStopAndPause,Map uv_uc,String geoValue,Map<String, Long> ClickByDomainDate,Map<String, Integer> PriceByDomain,Map<String, String> ClickAndMoneyByCamp) throws ParseException
    {
            StringBuilder sb = new StringBuilder();            
            int clickCharge = 0 ;
            int fraud_click = 0 ;
            String uc ="";
            String uv ="";
            String mb = "";
            String mt = "";
            String mn = "";
            String other = "";
          
            if(uv_uc.containsKey(Integer.valueOf(banId)))
            {
                String uValue = (String) uv_uc.get(Integer.valueOf(banId));
                uc = uValue.split("_")[0];
                uv = uValue.split("_")[1];
            } else {
                uc = "0";
                uv = "0";
            }
            
            if(geoValue != null)
            {
                mb = geoValue.split("_")[0];
                mt = geoValue.split("_")[1];
                mn = geoValue.split("_")[2];
                other = geoValue.split("_")[3];                
            } else{
                mb = "0";
                mt = "0";
                mn = "0";
                other = "0";
            }
            /*
             * calculate clicks from invalid geo
             */
            //int clickGeoInvalid = handleForGeo(mb, mt, mn, other, geoVal,totalclick);
            
            //if have fraudClick
            if(fraudclicks.containsKey(Integer.valueOf(banId)+"_"+currentDay))
            {                                
                /*
                 * Lay ra so tien cua campaign ung voi banner
                 * Sau do xem so tien do co vuot qua 20% khong
                 */                                                                
                fraud_click = (int)fraudclicks.get(Integer.valueOf(banId)+"_"+currentDay);
                
                long maxValueDay = 0;
                
                if(MaxValue.containsKey(Integer.valueOf(camId)))
                {
                    maxValueDay = MaxValue.get(Integer.valueOf(camId));
                }
                
                long moneyByBannerid = MoneyByBanner.get(banId);
                
                String clickbyCamp = ClickAndMoneyByCamp.get(camId+"_"+currentDay);
                    
                String[] valueCap = new String[2];
                valueCap = clickbyCamp.split("_");
                //money cua campaignid
                int moneyByCamID = Integer.valueOf(valueCap[1]);
                //if vuot qua 1.2 lan ngan sach thi tinh them 20%
                //clickcharge lay tu realclick - so click phan du so voi 120% ngan sach tu bang domaindate
                //so tien se la 120% ngan sach                
                if(moneyByCamID >= 1.2*maxValueDay)
                {
                    float sum = Float.valueOf(mb) +Float.valueOf(mn) +Float.valueOf(mt) +Float.valueOf(other);
                    //so du se dung de tinh clickcharge
                    long diffMoney = (long) (moneyByCamID - 1.2 * maxValueDay);
                    //tinh toan clickcharge
                    clickCharge = getClickChargeByBannerid(diffMoney, currentDay,Integer.valueOf(camId), totalclick-fraud_click,ClickAndMoneyByCamp);
                    
                    int moneyCharge = 0 ;   
                    //tinh moneycharge neu co clickcharge
                    if((totalclick-fraud_click) != 0){
                        moneyCharge = (int)((moneyByBannerid*(totalclick-fraud_click-clickCharge))/(totalclick-fraud_click));
                    } else {
                        moneyCharge = 0;
                    }                    
                    
                    if(sum == 0)
                    {
                        String insertData = "('" + camId + "'," + banId +"," + totalclick +"," + (totalclick-fraud_click) +","+ (totalclick-fraud_click-clickCharge) +","+ totalview + "," + totalview +","+ Integer.valueOf(uc) + "," + Integer.valueOf(uv) +",0,0,0,0,'" + currentDay + "'," + moneyCharge +")";
                        sb.append(insertData).append(",");
                    } else{
                        DecimalFormat df = new DecimalFormat("0.0#");

                        String mienbac = df.format(Float.valueOf(mb)/sum*100);
                        String mientrung = df.format(Float.valueOf(mt)/sum*100);
                        String miennam = df.format(Float.valueOf(mn)/sum*100);
                        String remain = df.format(Float.valueOf(other)/sum*100);

                        String insertData = "('" + camId + "'," + banId +"," + totalclick +"," + (totalclick-fraud_click) +","+ (totalclick-fraud_click-clickCharge) +","+ totalview + "," + totalview +","+ Integer.valueOf(uc) + "," + Integer.valueOf(uv) +","+ mienbac+","+ mientrung+","+ miennam+","+ remain +",'" + currentDay + "'," + moneyCharge +")";
                        sb.append(insertData).append(",");
                    }                    
                } else{
                    float sum = Float.valueOf(mb) +Float.valueOf(mn) +Float.valueOf(mt) +Float.valueOf(other);
                    if(totalclick-fraud_click <= 0)
                    {
                        if(sum == 0)
                        {
                            String insertData = "('" + camId + "'," + banId +"," + totalclick +"," + 0 +","+ 0 +","+ totalview + "," + totalview +","+ Integer.valueOf(uc) + "," + Integer.valueOf(uv) +",0,0,0,0,'" + currentDay + "'," + 0 +")";
                            sb.append(insertData).append(",");
                        } else{
                            DecimalFormat df = new DecimalFormat("0.0#");

                            String mienbac = df.format(Float.valueOf(mb)/sum*100);
                            String mientrung = df.format(Float.valueOf(mt)/sum*100);
                            String miennam = df.format(Float.valueOf(mn)/sum*100);
                            String remain = df.format(Float.valueOf(other)/sum*100);

                            String insertData = "('" + camId + "', " + banId +"," + totalclick +"," + 0 +","+ 0 +","+ totalview + "," + totalview +","+ Integer.valueOf(uc) + "," + Integer.valueOf(uv) +","+ mienbac+","+ mientrung+","+ miennam+","+ remain +",'" + currentDay + "'," + 0 +")";
                            sb.append(insertData).append(",");
                        }  
                    } else {
                        if(sum == 0)
                        {
                            String insertData = "('" + camId + "'," + banId +"," + totalclick +"," + (totalclick-fraud_click) +","+ (totalclick-fraud_click-clickCharge) +","+ totalview + "," + totalview +","+ Integer.valueOf(uc) + "," + Integer.valueOf(uv) +",0,0,0,0,'" + currentDay + "'," + moneyByBannerid +")";
                            sb.append(insertData).append(",");
                        } else{
                            DecimalFormat df = new DecimalFormat("0.0#");

                            String mienbac = df.format(Float.valueOf(mb)/sum*100);
                            String mientrung = df.format(Float.valueOf(mt)/sum*100);
                            String miennam = df.format(Float.valueOf(mn)/sum*100);
                            String remain = df.format(Float.valueOf(other)/sum*100);

                            String insertData = "('" + camId + "', " + banId +"," + totalclick +"," + (totalclick-fraud_click) +","+ (totalclick-fraud_click-clickCharge) +","+ totalview + "," + totalview +","+ Integer.valueOf(uc) + "," + Integer.valueOf(uv) +","+ mienbac+","+ mientrung+","+ miennam+","+ remain +",'" + currentDay + "'," + moneyByBannerid +")";
                            sb.append(insertData).append(",");
                        } 
                        
                    }
                }                               
            } else {
                if(ClickAndMoneyByCamp.containsKey(camId+"_"+currentDay)){
                    String clickbyCamp = ClickAndMoneyByCamp.get(camId+"_"+currentDay);
                    String[] valueCap = new String[2];
                    valueCap = clickbyCamp.split("_");
                    //money cua campaignid
                    int moneyByCamID = Integer.valueOf(valueCap[1]);
                    long maxValueDay = 0;
                    if(MaxValue.containsKey(Integer.valueOf(camId)))
                    {
                        maxValueDay = MaxValue.get(Integer.valueOf(camId));
                    }
                    long moneyByBannerid = 0;
                    
                    if(MoneyByBanner.containsKey(banId)){
                        moneyByBannerid = MoneyByBanner.get(banId);   
                    }
                    if(moneyByCamID >= 1.2*maxValueDay)
                    {
                        float sum = Float.valueOf(mb) +Float.valueOf(mn) +Float.valueOf(mt) +Float.valueOf(other);
                        //so du se dung de tinh clickcharge
                        long diffMoney = (long) (moneyByCamID - 1.2 * maxValueDay);
                        //tinh toan clickcharge
                        clickCharge = getClickChargeByBannerid(diffMoney, currentDay,Integer.valueOf(camId), totalclick,ClickAndMoneyByCamp);
                        int moneyCharge = 0 ;

                        if(totalclick != 0){
                            moneyCharge = (int)(moneyByBannerid*(totalclick-clickCharge))/totalclick;
                        } else {
                            moneyCharge = 0;
                        }
                        
                        if(sum == 0)
                        {
                            String insertData = "('" + camId + "'," + banId +"," + totalclick +"," + (totalclick) +","+ (totalclick-clickCharge) +","+ totalview + "," + totalview +","+ Integer.valueOf(uc) + "," + Integer.valueOf(uv) +",0,0,0,0,'" + currentDay + "'," + moneyCharge +")";
                            sb.append(insertData).append(",");
                        } else{
                            DecimalFormat df = new DecimalFormat("0.0#");

                            String mienbac = df.format(Float.valueOf(mb)/sum*100);
                            String mientrung = df.format(Float.valueOf(mt)/sum*100);
                            String miennam = df.format(Float.valueOf(mn)/sum*100);
                            String remain = df.format(Float.valueOf(other)/sum*100);

                            String insertData = "('" + camId + "'," + banId +"," + totalclick +"," + (totalclick) +","+ (totalclick-clickCharge) +","+ totalview + "," + totalview +","+ Integer.valueOf(uc) + "," + Integer.valueOf(uv) +","+ mienbac+","+ mientrung+","+ miennam+","+ remain +",'" + currentDay + "'," + moneyCharge +")";
                            sb.append(insertData).append(",");
                        }
                    } else{
                        float sum = Float.valueOf(mb) +Float.valueOf(mn) +Float.valueOf(mt) +Float.valueOf(other);
                        if(sum == 0)
                        {
                            String insertData = "('" + camId + "'," + banId +"," + totalclick +"," + (totalclick) +","+ (totalclick-clickCharge) +","+ totalview + "," + totalview +","+ Integer.valueOf(uc) + "," + Integer.valueOf(uv) +",0,0,0,0,'" + currentDay + "'," + moneyByBannerid +")";
                            sb.append(insertData).append(",");
                        } else{
                            DecimalFormat df = new DecimalFormat("0.0#");

                            String mienbac = df.format(Float.valueOf(mb)/sum*100);
                            String mientrung = df.format(Float.valueOf(mt)/sum*100);
                            String miennam = df.format(Float.valueOf(mn)/sum*100);
                            String remain = df.format(Float.valueOf(other)/sum*100);

                            String insertData = "('" + camId + "'," + banId +"," + totalclick +"," + (totalclick) +","+ (totalclick-clickCharge) +","+ totalview + "," + totalview +","+ Integer.valueOf(uc) + "," + Integer.valueOf(uv) +","+ mienbac+","+ mientrung+","+ miennam+","+ remain +",'" + currentDay + "'," + moneyByBannerid +")";
                            sb.append(insertData).append(",");
                        }
                    } 
                } 
            }
                        
            //mysqldatamanager.insertDataCampaignAndBanner(String.valueOf(camId),String.valueOf(banId), String.valueOf(countClick), String.valueOf(countView), (prefixDB +i));
            return sb.toString();
    }
    /*
     * totalMoney : lượng tiền chênh lệch giữa overbudget và tiền ban dau
     *
     */
    public static int getClickChargeByBannerid(long totalMoney,String currentDay,int campid,int realclick,Map<String, String> ClickAndMoneyByCamp)
    {        
        int retValue    = 0;
        int ret         = 0;
        String clickbyCamp = ClickAndMoneyByCamp.get(campid+"_"+currentDay);
        String[] valueCap = new String[2];
        valueCap = clickbyCamp.split("_");
        int click = Integer.valueOf(valueCap[0]);
        int money = Integer.valueOf(valueCap[1]);
        if(money != 0){            
            retValue = (int)(totalMoney*click/money);//so click charge theo campaing
            ret = (int) (retValue*realclick/click);//so click charge theo bannerid  
        }
                                                                                                                                                                                                                                                                                                                                                                                                                                          
        return ret;
    }
    
    public static int handleForGeo(String mb,String mt,String mn,String other,String geoValid,int totalclick){
        
        int relValue = 0 ;
        
        float retValue = 0;
        List geoList = new ArrayList();
        
        if(Integer.valueOf(mb) ==0 && Integer.valueOf(mt) ==0 && Integer.valueOf(mn) ==0){
            return relValue;
        }
        if(Integer.valueOf(mb) >0)
        {
            geoList.add("1");
        }
        if(Integer.valueOf(mt) >0){
            geoList.add("2");
        } 
        if(Integer.valueOf(mn) >0){
            geoList.add("3");
        }
        if(geoValid == null) return relValue;
        
        String[] geoArr = new String[2];
        geoArr = geoValid.split(",");
        
        for(int i = 0; i < geoArr.length ;i++ ){
           geoList.remove(geoArr[i]); 
        }        
        
        if(geoList.isEmpty())
        {
            retValue = 0;
        } else {            
            int sum = Integer.valueOf(mb) +Integer.valueOf(mn) +Integer.valueOf(mt) +Integer.valueOf(other);
            
            for (int i=0 ; i<geoList.size(); i++) {
                    String value = (String) geoList.get(i);
                    
                    if(value == "1") {
                        retValue = retValue + (float)Integer.valueOf(mb)/sum;
                    }
                    
                    if(value == "2") {
                        retValue = retValue + (float)Integer.valueOf(mt)/sum;
                    }
                    
                    if(value == "3") {
                        retValue = retValue + (float)Integer.valueOf(mn)/sum;
                    }
            }
            
        }
        relValue = (int)(retValue*(float)totalclick);
        
        if(relValue > 0) 
        {
          System.out.println(relValue);  
          Debug.print("ok");
        }
        
        return relValue;
    }
    
    
}
